from django.contrib import admin
from .models import Order, OrderReview


@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    """订单管理"""
    
    list_display = (
        'id', 'order_no', 'book', 'buyer', 'seller', 
        'price', 'status', 'payment_method', 'created_at'
    )
    list_filter = ('status', 'payment_method', 'created_at')
    search_fields = ('order_no', 'book__title', 'buyer__username', 'seller__username')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('订单信息', {
            'fields': ('order_no', 'book', 'buyer', 'seller')
        }),
        ('交易信息', {
            'fields': ('price', 'status', 'payment_method', 'payment_time')
        }),
        ('交付信息', {
            'fields': ('pickup_location', 'pickup_time', 'notes')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at', 'completed_at', 'cancelled_at')
        }),
        ('AI聊天', {
            'fields': ('chat_summary',),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ('created_at', 'updated_at')
    list_per_page = 20
    date_hierarchy = 'created_at'


@admin.register(OrderReview)
class OrderReviewAdmin(admin.ModelAdmin):
    """订单评价管理"""
    
    list_display = ('id', 'order', 'reviewer', 'rating', 'created_at')
    list_filter = ('rating', 'created_at')
    search_fields = ('order__order_no', 'reviewer__username', 'comment')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('评价信息', {
            'fields': ('order', 'reviewer', 'rating', 'comment', 'tags')
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 20
    date_hierarchy = 'created_at'
